package com.example.rabbitmq.controller.dlx;

import com.example.rabbitmq.Constants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//@RestController
//@RequestMapping("/producer")
public class DlxProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

//    @RequestMapping("/normal")
//    public String normal() {
//        CorrelationData correlationData = new CorrelationData("6");
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal",
//                "normal test 20s..." + new Date(), new MessagePostProcessor() {
//                    @Override
//                    public Message postProcessMessage(Message message) throws AmqpException {
//                        message.getMessageProperties().setExpiration("20000");
//                        return message;
//                    }
//                });
//
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal",
//                "normal test 10s..." + new Date(), new MessagePostProcessor() {
//                    @Override
//                    public Message postProcessMessage(Message message) throws AmqpException {
//                        message.getMessageProperties().setExpiration("10000");
//                        return message;
//                    }
//                });
//        return "发送消息成功";
//    }

    @RequestMapping("/normal")
    public String normal() {
        CorrelationData correlationData = new CorrelationData("6");

        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal",
                "normal test 10s..." + new Date(), new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration("10000");
                        return message;
                    }
                });
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal",
                "normal test 20s..." + new Date(), new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration("20000");
                        return message;
                    }
                });
        return "发送消息成功";
    }

    @RequestMapping("/normal1")
    public String normal1() {
        for(int i = 0; i < 10; i++) {
            CorrelationData correlationData = new CorrelationData(i + " ");
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal",
                    "normal1 test...",correlationData);
        }
        return "发送消息成功1";
    }
}
